<template>
  <div class="category">
    <el-breadcrumb>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-button type="primary" @click="showCateDialog">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表 -->
      <el-table
        :data="cateList"
        row-key="cat_id"
        :tree-props="{ children: 'children' }"
        stripe
      >
        <el-table-column label="分类名称" prop="cat_name"></el-table-column>
        <el-table-column>
          <template #default="scope">
            <i
              class="el-icon-success"
              style="color:lightgreen"
              v-if="scope.row.cat_deleted"
            ></i>
            <i class="el-icon-error" style="color:red" v-else></i>
          </template>
        </el-table-column>
        <el-table-column>
          <template #default="scope">
            <el-tag v-if="scope.row.cat_level == 0" type="primary">一级</el-tag>
            <el-tag v-else-if="scope.row.cat_level == 1" type="success"
              >二级</el-tag
            >
            <el-tag v-else type="warning">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column>
          <el-button type="primary" size="mini">编辑</el-button>
          <el-button type="danger" size="mini">删除</el-button>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageSizeInfo.pagenum"
        :page-size="pageSizeInfo.pagesize"
        :page-sizes="[1, 3, 5, 7]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="cateDialogVisible"
      @close="cateDialogClose"
      width="50%"
    >
      <el-form
        :model="cateDialogFormData"
        :rules="cateDialogFormRules"
        ref="cateDialogFormRef"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input
            v-model="cateDialogFormData.cat_name"
            @input="test"
          ></el-input>
        </el-form-item>
        <el-form-item label="父类名称">
          <el-cascader
            @change="casSelected"
            :options="dialogAllCate"
            :props="cascaderProps"
            clearable
            v-model="cascaderSelectedList"
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="cateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmAddCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "category",
  data() {
    return {
      cateList: [], //分类数据
      total: 0, //分类数据总条数
      pageSizeInfo: {
        //到后台请求数据的参数
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cateDialogVisible: false, //分类对话框的显示和隐藏

      //添加分类对话框提交到后台的表单数据
      cateDialogFormData: {
        cat_name: "",
        cat_pid: 0, //父亲的id 如果是第一级分类则设置为0
        cat_level: 0 //自己所处的层级
      },
      //对话框表单验证规则
      cateDialogFormRules: {
        cat_name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },

          {
            min: 2,
            max: 8,
            message: "商品分类名称不符合规范,长度应为2-8位",
            trigger: "blur"
          }
        ]
      },
      dialogAllCate: [],

      //定义级联选择器数据接收的接口 以及相关配置
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        expandTrigger: "hover",
        checkStrictly: true
      },
      cascaderSelectedList: []
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    //获取分类列表
    getCateList() {
      this.$http
        .get("categories", {
          params: this.pageSizeInfo
        })
        .then(res => {
          if (res.data.meta.status == 200) {
            (this.cateList = res.data.data.result), console.log(res.data);
            this.total = res.data.data.total;
          }
        });
    },
    //处理分页
    handleSizeChange(newSize) {
      this.pageSizeInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(newPage) {
      this.pageSizeInfo.pagenum = newPage;
      this.getCateList();
    },
    //点击添加分类展示对话框
    showCateDialog() {
      this.cateDialogVisible = true;
      this.$http
        .get("categories", {
          params: {
            type: 2
          }
        })
        .then(res => {
          if (res.data.meta.status == 200) {
            this.dialogAllCate = res.data.data;
          } else {
            this.$message.error("获取商品分类数据失败");
          }
        })
        .catch(err => err);
    },
    test() {
      console.log(this.cateDialogFormData.cat_name);
    },
    casSelected(value) {
      this.cascaderSelectedList = value;
      //大于0 表示选择了级联选择器
      console.log(this.cascaderSelectedList);
      if (this.cascaderSelectedList.length > 0) {
        this.cateDialogFormData.cat_level = this.cascaderSelectedList.length;
        this.cateDialogFormData.cat_pid = this.cascaderSelectedList[
          this.cascaderSelectedList.length - 1
        ];
      } else {
        this.cateDialogFormData.cat_level = 0;
        this.cateDialogFormData.cat_pid = 0;
      }
    },
    //对话框关闭要做重置表单
    cateDialogClose() {
      this.$refs.cateDialogFormRef.resetFields();
      this.cascaderSelectedList = []; //将选择的级联器数据清空
      //将提交到后台的数据重置 防止用户下次加分类的时候直接写个分类名就提交
      this.cateDialogFormData.cat_pid = 0;
      this.cateDialogFormData.cat_level = 0;
    },
    //确认添加分类
    confirmAddCate() {
      this.$refs.cateDialogFormRef.validate(valid => {
        if (valid) {
          this.$http
            .post("categories", this.cateDialogFormData)
            .then(res => {
              console.log(res);
              if (res.data.meta.status == 201) {
                this.getCateList();
                this.cateDialogVisible = false;
                this.$message.success("添加分类成功");
              } else {
                this.$message.error("添加失败");
              }
            })
            .catch(err => err);
        } else {
          return;
        }
      });
      //提交数据
    }
  }
};
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 20px;
}
</style>
